package com.smec.moblie.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.Window;
import android.widget.Toast;

import com.smec.moblie.R;
import com.smec.moblie.flyn.Eyes;

/**
 * android6.0之后版本获取权限，之前版本直接打开登入页
 * Created by sunyubo on 16/12/21.
 */
public class PermissionActivity extends Activity {
    public final static int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
    public final static int MY_PERMISSIONS_REQUEST_CAMERA = 2;
    public final static int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 3;
    public final static int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 4;
    public final static int REQUEST_PERMISSION_SETTING = 20007;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 隐去标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remes_act_login);

//        setStatusBarLightMode(this, android.R.color.transparent);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//        }
        Eyes.translucentStatusBar(PermissionActivity.this, true);

        //判断版本是否为6.0之后版本
        if (Build.VERSION.SDK_INT >= 23) {
            checkAppPermission();
        } else {
            startToLogin();
        }
    }

    private void checkAppPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //申请WRITE_EXTERNAL_STORAGE权限（文件操作）
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            //申请CAMERA权限（摄像头权限）
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //申请ACCESS_FINE_LOCATION权限（定位权限）
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            //申请READ_PHONE_STATE权限（手机状态权限）
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                    MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
        } else {
            startToLogin();
        }
    }

    //权限请求监听
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        if (requestCode == MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) {
            //判断用户是否选择允许
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    //申请CAMERA权限（摄像头权限）
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                            MY_PERMISSIONS_REQUEST_CAMERA);
                } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    //申请ACCESS_FINE_LOCATION权限（定位权限）
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
                } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    //申请READ_PHONE_STATE权限（手机状态权限）
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                            MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
                } else {
                    startToLogin();
                }
            } else {
                // Permission Denied 用户选择拒绝
                Toast.makeText(PermissionActivity.this, "获取文件权限失败，无法使用应用", Toast.LENGTH_SHORT).show();
                gotoSetting(requestCode, permissions, grantResults);
                PermissionActivity.this.finish();
            }
            return;
        } else if (requestCode == MY_PERMISSIONS_REQUEST_CAMERA) {
            //判断用户是否选择允许
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    //申请ACCESS_FINE_LOCATION权限（定位权限）
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
                } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    //申请READ_PHONE_STATE权限（手机状态权限）
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                            MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
                } else {
                    startToLogin();
                }
            } else {
                // Permission Denied 用户选择拒绝
                Toast.makeText(PermissionActivity.this, "获取拍照权限失败，无法使用应用", Toast.LENGTH_SHORT).show();
                gotoSetting(requestCode, permissions, grantResults);
                PermissionActivity.this.finish();
            }
            return;
        } else if (requestCode == MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) {
            //判断用户是否选择允许
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    //申请READ_PHONE_STATE权限（手机状态权限）
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                            MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
                } else {
                    startToLogin();
                }
            } else {
                // Permission Denied 用户选择拒绝
                Toast.makeText(PermissionActivity.this, "获取定位权限失败，无法使用应用", Toast.LENGTH_SHORT).show();
                gotoSetting(requestCode, permissions, grantResults);
                PermissionActivity.this.finish();
            }
            return;
        } else if (requestCode == MY_PERMISSIONS_REQUEST_READ_PHONE_STATE) {
            //判断用户是否选择允许
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startToLogin();
            } else {
                // Permission Denied 用户选择拒绝
                Toast.makeText(PermissionActivity.this, "获取手机状态权限失败，无法使用应用", Toast.LENGTH_SHORT).show();
                gotoSetting(requestCode, permissions, grantResults);
                PermissionActivity.this.finish();
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void gotoSetting(int requestCode, String[] permissions, int[] grantResults) {
        for (int i = 0; i < grantResults.length; i++) {
            //判断权限的结果，如果有被拒绝，就return
            if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                //判断是否勾选了不再提示，如果勾选直接跳转到系统应用界面让用户手动开启权限
                if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {
                    Toast.makeText(PermissionActivity.this, "请点击权限后开启应用全部权限，否则无法使用", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

                    Uri uri = Uri.fromParts("package", getPackageName(), null);

                    intent.setData(uri);

                    startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                    PermissionActivity.this.finish();
                }
            }
        }
    }

    /**
     * 跳转到登入页
     */
    private void startToLogin() {
        Intent intent = new Intent(PermissionActivity.this, LoginActivity.class);
        startActivity(intent);
        PermissionActivity.this.finish();
    }
}
